package ma.ensao.evalmetrics.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import ma.ensao.evalmetrics.model.ResultMethod;
import ma.ensao.evalmetrics.util.HibernateUtil;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.classic.Session;
public class MethodManager {
	
	public int countTotal(Long id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		int count = ((Long)session.createQuery("select count(*) from ResultMethod where idProjet="+id).uniqueResult()).intValue();
		session.getTransaction().commit();
		return count;
	}
	
	@SuppressWarnings("unchecked")
	public List<ResultMethod> listAll(Long id,int startIndex, int pageSize,String jtSorting) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ResultMethod> methode = null;
		try {
			Query q = session.createQuery("from ResultMethod where idProjet="+id+" ORDER BY "+jtSorting+" ");
			q.setFirstResult(startIndex);
			q.setMaxResults(pageSize);
			methode = (List<ResultMethod>)q.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return methode;
	}
	
public List<ResultMethod> list(Long id,int startIndex, int pageSize,String jtSorting) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ResultMethod> methode = null;
		try {
			Query q = session.createQuery("from ResultMethod where idProjet="+id+" AND idtest IN (SELECT MAX( idtest ) FROM ResultMethod  where idProjet="+id+" ) ORDER BY "+jtSorting+" ");
			q.setFirstResult(startIndex);
			q.setMaxResults(pageSize);
			methode = (List<ResultMethod>)q.list();
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return methode;
	}
	public List<ResultMethod> add(List<ResultMethod> method) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		for ( int i=0; i<method.size(); i++ ) {
			
		session.save(method.get(i));
		}
		session.getTransaction().commit();
		return method;
	}
	public ResultMethod addMethod(ResultMethod method) {
		int lastid = testValue(method.getIdProjet());
		method.setIdtest(lastid);	
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(method);
		
		session.getTransaction().commit();
		return method;
	}
	
	public void updateMethod(ResultMethod method) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.update(method);
		
		session.getTransaction().commit();
	}
	
	public void deleteMethod(ResultMethod method) {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(method);
		session.getTransaction().commit();
		
	}
	
	public int testValue(Long id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		int idtest=0;
		try {
			Query query = session.createQuery("from ResultMethod  where idProjet="+id+" AND  idtest IN (SELECT MAX( idtest ) from ResultMethod  where idProjet="+id+" )");
			Iterator<?> methodresult = query.iterate();
		
			if (methodresult.hasNext()) {
		
			ResultMethod m = (ResultMethod) methodresult.next();
			idtest=m.getIdtest();
			
			}
		
			
		} catch (HibernateException e) {
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		session.getTransaction().commit();
		return idtest;
		
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException {

		 
	}
	

}